use crate::http::server::error::Error;
use crate::http::server::handler::result::IntoJsonResponseResult;
use crate::http::server::middleware::ValidatedJson;
use crate::{controller, dto};
use axum::Json;
use axum::extract::State;
use std::sync::Arc;

pub struct CandidatePathHandler {
    controller_set: Arc<controller::ControllerSet>,
}

impl CandidatePathHandler {
    pub fn new(controller_set: Arc<controller::ControllerSet>) -> Self {
        CandidatePathHandler { controller_set }
    }

    pub async fn add(
        State(state): State<Arc<Self>>,
        request: ValidatedJson<dto::candidate_path::AddRequest>,
    ) -> Result<
        Json<wanmesh_common::dto::Response<dto::core::candidate_path::AddResponseData>>,
        Error,
    > {
        state
            .controller_set
            .candidate_path
            .add(&request.0)
            .await
            .into_json_response_result()
    }

    pub async fn list(
        State(state): State<Arc<Self>>,
        request: ValidatedJson<dto::candidate_path::ListRequest>,
    ) -> Result<Json<wanmesh_common::dto::Response<dto::candidate_path::ListResponseData>>, Error>
    {
        state
            .controller_set
            .candidate_path
            .list(&request.0)
            .await
            .into_json_response_result()
    }

    pub async fn list_info(
        State(state): State<Arc<Self>>,
        request: ValidatedJson<dto::candidate_path::ListRequest>,
    ) -> Result<
        Json<wanmesh_common::dto::Response<dto::candidate_path::ListInfoResponseData>>,
        Error,
    > {
        state
            .controller_set
            .candidate_path
            .list_info(&request.0)
            .await
            .into_json_response_result()
    }


    pub async fn get(
        State(state): State<Arc<Self>>,
        request: ValidatedJson<dto::candidate_path::GetRequest>,
    ) -> Result<
        Json<wanmesh_common::dto::Response<dto::core::candidate_path::CandidatePathItem>>,
        Error,
    > {
        state
            .controller_set
            .candidate_path
            .get(&request.0)
            .await
            .into_json_response_result()
    }

    pub async fn get_info(
        State(state): State<Arc<Self>>,
        request: ValidatedJson<dto::candidate_path::GetRequest>,
    ) -> Result<
        Json<wanmesh_common::dto::Response<dto::candidate_path::CandidatePathInfo>>,
        Error,
    > {
        state
            .controller_set
            .candidate_path
            .get_info(&request.0)
            .await
            .into_json_response_result()
    }


    pub async fn update(
        State(state): State<Arc<Self>>,
        request: ValidatedJson<dto::candidate_path::UpdateRequest>,
    ) -> Result<
        Json<wanmesh_common::dto::Response<()>>,
        Error,
    > {
        state
            .controller_set
            .candidate_path
            .update(&request.0)
            .await
            .into_json_response_result()
    }

    pub async fn delete(
        State(state): State<Arc<Self>>,
        request: ValidatedJson<dto::candidate_path::DeleteRequest>,
    ) -> Result<
        Json<wanmesh_common::dto::Response<()>>,
        Error,
    > {
        state
            .controller_set
            .candidate_path
            .delete(&request.0)
            .await
            .into_json_response_result()
    }
}
